<?php

namespace App\Models\Factory\V0;


use App\Constants\InviteConstant;
use App\Models\AbsModelFactory;
use App\Models\Orm\V0\Query;
use App\Models\Orm\V0\QueryDF;
use App\Models\Orm\V0\QueryFont;
use App\Models\Orm\V0\QueryTotal;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

/**
 * 交易
 */
class QueryFactory extends AbsModelFactory
{

    //同步通知插入有积分交易表
    public static  function  addQuery($data)
    {
        $result = Query::firstOrcreate(['merOrderId'=>$data['merOrderId']],$data);
        return $result ? true :false;
    }


    //同步通知插入无积分交易表
    public static  function  addQueryFont($data)
    {
        $result = QueryFont::firstOrCreate(['orderId'=>$data['orderId']],$data);
        return $result ? true :false;
    }

    //同步插入交易总表
    public static function addQueryTotal($data)
    {
        $result = QueryTotal::updateOrCreate(['order_id'=>$data['order_id']],$data);
        return $result ? true :false;
    }


    //同步通知插入有代付交易表
    public static  function  addQueryDF($data)
    {
        $result = QueryDF::updateOrCreate(['orderId'=>$data['orderId']],$data);
        return $result ? true :false;
    }

    //更新无积分的消费通知
    public static  function updateQueryFont($info)
    {

//        $result = QueryFont::where('orderId',$info['orderId'])->update($info);
        $result = QueryFont::updateOrCreate(['orderId'=>$info['orderId']],$info);


        return $result ? true : false;
    }

    //更新有积分的消费通知
    public static  function updateQuery($info)
    {

//        $result = Query::where('merOrderId',$info['merOrderId'])->update($info);
        $result = Query::updateOrCreate(['merOrderId'=>$info['merOrderId']],$info);

        return $result ? true : false;
    }


    //更新有积分的代付通知
    public static  function updateQueryDF($info)
    {

//        $result = QueryDF::where('orderId',$info['orderId'])->where('queryID',$info['queryID'])->where('user_id',$info['user_id'])->update($info);
        $result = QueryDF::updateOrCreate(['orderId'=>$info['orderId']],$info);

        return $result ? true : false;
    }


    //获取订单列表
    public static function orderList($user_id)
    {
        $result =QueryTotal::where('user_id',$user_id)->orderBy('created_time','desc')->get()->toArray();
        return $result ? $result : '';
    }

    //获取最新订单
    public static function order($user_id)
    {
        $result =QueryTotal::where('user_id',$user_id)->orderBy('created_time','desc')->first();
        return $result ? $result->toArray() : '';
    }

    //获取无积分最新订单
    public static function getOrderStatus($user_id,$orderId)
    {
        $result =QueryFont::where('user_id',$user_id)->where('orderId',$orderId)->first();
        return $result ? $result : '';
    }

    //获取有积分
    public static function getOrderQueryStatus($user_id,$orderId)
    {
        $result =Query::where('user_id',$user_id)->where('merOrderId',$orderId)->first();
        return $result ? $result : '';
    }
}